package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class BibliotecaApp {

    List<Book> books = new ArrayList<Book>();

    public static void main(String[] args) {
        BibliotecaApp app = new BibliotecaApp();
        app.startApp();
    }

    public void startApp() {
        int option = -1;
        System.out.print(displayWelcome());

        System.out.println("Available Books");
        System.out.println("-----------------");
        List<Book> availableBooks = getAvailableBooks();
        for(Book b : availableBooks)
            System.out.println(b.getName());

        do {
            System.out.println();
            System.out.println("\nMAIN MENU");
            System.out.println("============");
            for(String o : getMenuOptions())
                System.out.println(o);

            System.out.println();
            System.out.print("Choose an option ==> ");

            Scanner in = new Scanner(System.in);

            if(in.hasNextInt()) {
                option = in.nextInt();
                System.out.print(selectOption(option));
            } else {
                System.out.println("Select a valid option!");
            }
        } while (option != 0);
    }

    protected String displayWelcome() {
        return "Welcome to Biblioteca!\n" +
            "==========================\n\n";
    }

    private String listBooks() {
        clearScreen();
        return "Books\n" +
               "========";
    }

    private void clearScreen() {
        for(int i = 0; i < 1000; i++)
            System.out.println();
    }

    protected List<Book> getAvailableBooks() {
        List<Book> availableBooks = new ArrayList<Book>();

        for(Book b : books) {
            if(b.getStatus() == BookStatus.AVAILABLE)
                availableBooks.add(b);
        }

        return availableBooks;
    }

    protected String[] getMenuOptions() {
        String[] options = { "1 - List Books", "0 - Exit" };
        return options;
    }

    public String selectOption(int option) {
        switch (option) {
            case 0: return "";
            case 1:
                return listBooks();
            default: return "Select a valid option!";
        }
    }

    public String checkoutBook(Book bookToCheckout, Customer customer) {
        Book book = books.get(books.indexOf(bookToCheckout));

        if(book.getStatus() == BookStatus.AVAILABLE) {
            book.setStatus(BookStatus.UNAVAILABLE);
            customer.addBook(book);
            return "Thank you! Enjoy the book.";
        } else {
            return "That book is not available.";
        }
    }

    public Book getBook(Book bookToSearch) {
        Book book = books.get(books.indexOf(bookToSearch));

        if(book.getStatus() == BookStatus.AVAILABLE)
            return book;
        else
            return null;
    }

    public void addBookToLibrary(Book book) {
        books.add(book);
    }
}
